package com.ott.tool;

import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;

import com.mysql.jdbc.Connection;
import com.mysql.jdbc.PreparedStatement;

public class JDBCHelp {

	/**
	 * JDBC连接数据库
	 * @return
	 */
	public static Connection getConn() {
	    String driver = "com.mysql.jdbc.Driver";
	    String url = "jdbc:mysql://localhost:3306/tms?useUnicode=true&characterEncoding=utf-8&useSSL=false";
	    String username = "root";
	    String password = "root";
	    Connection conn = null;
	    try {
	    	//IndexForm.textArea.append("==========开始连接数据库=========\r\n");
	        Class.forName(driver); //classLoader,加载对应驱动
	        conn = (Connection) DriverManager.getConnection(url, username, password);
	       // IndexForm.textArea.append("==========数据库连接成功=========\r\n");
	    } catch (ClassNotFoundException e) {
	        e.printStackTrace();
	       // IndexForm.textArea.append("==========数据库连接失败=========\r\n");
	    } catch (SQLException e) {
	        e.printStackTrace();
	        //IndexForm.textArea.append("==========数据库连接失败=========\r\n");
	    }
	    return conn;
	}
	
	
	/**
	 * 查询cinema_code
	 * @return
	 */
	public static String findCinemaCode(){
		Connection conn =JDBCHelp.getConn();
		 String sql = "select cinema_code from cinema limit 1";
		 String cinema_codeString = "";
	    PreparedStatement pstmt;
	    try {
	        pstmt = (PreparedStatement)conn.prepareStatement(sql);
	        ResultSet rs = pstmt.executeQuery();
	        while (rs.next()) {
	        	 cinema_codeString = rs.getString("cinema_code");
	        }
	    } catch (SQLException e) {
	        e.printStackTrace();
	    }
	    return cinema_codeString;
	}
	
	public static void main(String[] args) {
		String a  = findCinemaCode();
	}
}
